import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:webview_flutter/platform_interface.dart';
import 'package:webview_flutter/webview_flutter.dart';
const kAndroidUserAgent =
    'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36';

String baseUrl = 'https://dollfi.com?dv=';
String selectedUrl = '';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final flutterWebViewPlugin = FlutterWebviewPlugin();
  bool isSuccess = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    // flutterWebViewPlugin.onUrlChanged.listen((event) {
    //   print("onUrlChanged : $event");
    // });
    flutterWebViewPlugin.onProgressChanged.listen((event) {
      if (event != 0.1) {
        EasyLoading.init();
        EasyLoading.show(status: 'loading...', maskType: EasyLoadingMaskType.black, dismissOnTap:true);
      }
      if (event == 1.0) {
        setState(() {
          isSuccess = true;
        });
        EasyLoading.dismiss();
      }
      print("flutterWebViewPlugin onProgressChanged ${event}");
    });
    if (Platform.isIOS) {
      //ios相关代码
      selectedUrl =  baseUrl + 'ios';
    } else if (Platform.isAndroid) {
      //android相关代码
      selectedUrl = baseUrl + 'android';
    }
  }

  @override
  Widget build(BuildContext context) {
    print("加载路径: $selectedUrl");
    return MaterialApp(
      routes: {
        "/": (_) => SafeArea(
                // child:new WebviewScaffold(
                //   url: "https://dollfi.com",
                //   hidden: !isSuccess,
                // )
          child: WebView(initialUrl: selectedUrl,
            onWebResourceError: (WebResourceError error){
    if(EasyLoading.isShow) {
      EasyLoading.dismiss();
    }
            },
            onWebViewCreated: (WebViewController controller){
              print('onWebViewCreated');
              if(!EasyLoading.isShow) {
                EasyLoading.show(status: 'loading...',
                    maskType: EasyLoadingMaskType.black,
                    dismissOnTap: true);
              }
            },
            onPageStarted: (String url){
              // EasyLoading.init();
              // EasyLoading.dismiss();
              if(!EasyLoading.isShow) {
                EasyLoading.show(status: 'loading...',
                    maskType: EasyLoadingMaskType.black,
                    dismissOnTap: true);
              }
              print('onPageStarted: $url');
            },
            javascriptMode: JavascriptMode.unrestricted,
            onPageFinished: (String url){
            print('onPageFinish: $url');
            if(EasyLoading.isShow) {
              EasyLoading.dismiss();
            }
            },
            onProgress: (int progress){

            },
          ),
        ),
      },
      builder: EasyLoading.init(),
    );
  }
  
  List<Widget> getWidgets(BuildContext context){
    // if(isSuccess){
      return [
        new WebviewScaffold(
          url: selectedUrl,
          hidden: false,
        )
      ];
    // }else{
    //   return [
    //     new WebviewScaffold(
    //       url: selectedUrl,
    //       hidden: true,
    //     ),
    //     Image.asset('asset/image/launch_image.jpg',fit: BoxFit.fill),
    //   ];
    // }
  }
}
